/**
 * 
 */
package com.geekteam.manga.sync;

import android.content.Context;

import com.geekteam.manga.Utils.LogUtils;
import com.geekteam.manga.provider.MangaAccessor;

/**
 * @author MtRol
 *
 */
public class SyncProcess {
	private static final String TAG = LogUtils.makeLogTag(SyncProcess.class);
	
	private Context mContext;
	private MangaAccessor dbAccess;
	private static SyncProcess mInstance;
	
	public static SyncProcess getInstance(Context context){
		if(mInstance==null){
			synchronized(SyncProcess.class){
				if(mInstance==null){
					mInstance = new SyncProcess(context);
				}
			}
		}
		
		return mInstance;
	}
	
	private SyncProcess(Context context){
		mContext = context;
		dbAccess = new MangaAccessor(mContext);
	}
	
	/*public void updateSettingDatabase(ArrayList<ItemSetting> items, String keyword, int type){
		if(type < 0){
			for (ItemSetting item : items) {
				if (!item.updateDatabase(dbAccess)) {
					LogUtils.LOGE(TAG, "update database error: " + item.toString());
				}
			}
		}else{
			String category = "";
			String city = "";
			String site = "";
			for (ItemSetting item : items) {
				if (!item.updateDatabase(dbAccess)) {
					LogUtils.LOGE(TAG, "update database error: " + item.toString());
				} else {
					if (item.isCheck()) {
						if (item.getType() == ItemSetting.TYPE_CATEGORY) {
							category += "," + item.getModelId();
						} else if (item.getType() == ItemSetting.TYPE_CITY) {
							city += "," + item.getModelId();
						} else if (item.getType() == ItemSetting.TYPE_SITE) {
							site += "," + item.getModelId();
						}
					}
				}
			}

			LogUtils.LOGI(TAG, "before subString: category: "+category+". city: "+city+". site: "+site);
			if(!category.equals("")){
				category = category.substring(1);
			}
			if(!city.equals("")){
				city = city.substring(1);
			}
			if(!site.equals("")){
				site = site.substring(1);
			}
			LogUtils.LOGI(TAG, "after subString: category: "+category+". city: "+city+". site: "+site);
			
			Utils.pushSetting(keyword, type, category, city, site);
		}
		
		//delete all job not favorite
		dbAccess.deleteAllJobNotFavorite();
	}
	
	public ArrayList<CategoryModel> getListCategory(boolean isFavorite) {
		if (dbAccess == null) {
			return null;
		}
		List<Long> cateId = new ArrayList<Long>();
		cateId = dbAccess.getAllCategory(isFavorite);
		if (cateId == null) {
			return null;
		}
		ArrayList<CategoryModel> listCategory = new ArrayList<CategoryModel>();
		for (long id : cateId) {
			CategoryModel model = dbAccess.getCategory(id);
			if (model != null) {
				listCategory.add(model);
			}
		}
		
		return listCategory;
	}

	public ArrayList<CityModel> getListCity() {
		if (dbAccess == null) {
			return null;
		}
		List<Long> cityId = new ArrayList<Long>();
		cityId = dbAccess.getAllCity();
		if (cityId == null) {
			return null;
		}
		ArrayList<CityModel> listCity = new ArrayList<CityModel>();
		for (long id : cityId) {
			CityModel model = dbAccess.getCity(id);
			if (model != null) {
				listCity.add(model);
			}
		}
		
		return listCity;
	}

	public ArrayList<SiteModel> getListSite() {
		if (dbAccess == null) {
			return null;
		}
		List<Long> siteId = new ArrayList<Long>();
		siteId = dbAccess.getAllSite();
		if (siteId == null) {
			return null;
		}
		ArrayList<SiteModel> listSite = new ArrayList<SiteModel>();
		for (long id : siteId) {
			SiteModel model = dbAccess.getSite(id);
			if (model != null) {
				listSite.add(model);
			}
		}
		
		return listSite;
	}
	
	public ArrayList<JobModel> getListJob(boolean isFavorite){
		if (dbAccess == null) {
			return null;
		}
		List<Long> jobIds = new ArrayList<Long>();
		jobIds = dbAccess.getAllJob(isFavorite);
		if(jobIds==null){
			return null;
		}
		
		ArrayList<JobModel> listJob = new ArrayList<JobModel>();
		for(long id: jobIds){
			JobModel model = dbAccess.getJob(id);
			if(model!=null){
				listJob.add(model);
			}
		}
		
		return listJob;
	}
	
	public void insertListJob(Jobs jobs, boolean isFirstLoad){
		//xu ly viec delete olde database o day
		if(isFirstLoad){
			dbAccess.deleteAllJobNotFavorite();
		}
		if(jobs==null){
			return;
		}
		//insert data here
		dbAccess.writeJob(jobs);
	}
	
	public ArrayList<ParcelableNameValuePair> addParamGetJob(String lastId, int pageSize){
		SharedPreferences share = Utils.getSharedPreferences();
		String city = share.getString(Constant.CITY, "-1");
		String category = share.getString(Constant.CATEGORY, "-1");
		String site = share.getString(Constant.SITE, "-1");
		String keyword = share.getString(Constant.KEYWORD, "");
		int type = share.getInt(Constant.TYPE, SettingFragment.TYPE_ALL);
		
		ArrayList<ParcelableNameValuePair> result = new ArrayList<ParcelableNameValuePair>();
		result.add(new ParcelableNameValuePair(Constant.JOB_LAST_ID, lastId));
		result.add(new ParcelableNameValuePair(Constant.JOB_SIZE, ""+pageSize));
		result.add(new ParcelableNameValuePair(Constant.JOB_CATEGORY, category));
		result.add(new ParcelableNameValuePair(Constant.JOB_CITY, city));
		result.add(new ParcelableNameValuePair(Constant.JOB_SITE, site));
		result.add(new ParcelableNameValuePair(Constant.JOB_TYPE, ""+type));
		result.add(new ParcelableNameValuePair(Constant.KEYWORD, keyword));
		return result;
	}*/
}
